package com.gitee.magic.context.converter;

import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.converter.AbstractConverterEditor;
import com.gitee.magic.core.json.JsonObject;

/**
 * @author start
 */
public class ObjectvConverterEditor extends AbstractConverterEditor<String> {

	public ObjectvConverterEditor(Class<?> prototype) {
		super(prototype);
	}

	@Override
	public void restore(Object value) {
		if (value != null) {
			Class<?> target=value.getClass();
			JsonObject json;
			if(target.equals(JsonObject.class)) {
				json=(JsonObject)value;
			}else {
				json=new JsonObject(String.valueOf(value));
			}
			setValue(ConverterEditorUtils.restoreObject(getPrototype(), json));
		}
	}

	@Override
	public String converter() {
		if (getSource() == null) {
			return null;
		}
		return getSource().toString();
	}

	@Override
	public void setValue(Object v) {
		super.setValue(v);
		if(getValue()!=null) {
			setSource(ConverterEditorUtils.converterObject(v));
		}
	}

}
